<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * This page recive an actions for folder's
 *
 * @package   block_email_list
 * @author    Toni Mas <toni.mas@uib.dot.es>
 * @copyright 2006 by <toni.mas at uib dot es>
 * @license   The source code packaged with this file is Free Software, Copyright (C) 2006 by
 *            <toni.mas at uib dot es>.
 *            It's licensed under the AFFERO GENERAL PUBLIC LICENSE unless stated otherwise.
 *            You can get copies of the licenses here: http://www.affero.org/oagpl.html
 *            AFFERO GENERAL PUBLIC LICENSE is also included in the file called "COPYING".
 * @uses $CFG
 * @uses $COURSE
 * @uses $DB
 * @uses $OUTPUT
 */

global $CFG, $COURSE, $DB, $OUTPUT;

require_once('../../../config.php');
require_once($CFG->dirroot.'/blocks/email_list/email/lib.php');
require_once($CFG->dirroot.'/blocks/email_list/email/folder_form.php');

$id 		= optional_param('id', 0, PARAM_INT); 				// Folder Id.
$courseid 	= optional_param('course', SITEID, PARAM_INT);     // Course ID
$action 	= optional_param('action', '', PARAM_ALPHANUM);	// Action

// If defined course to view
if (! $course = $DB->get_record('course', array('id' => $courseid))) {
    print_error('invalidcourseid', 'block_email_list');
}

require_login($course->id, false); // No autologin guest

if (! email_is_enabled_email_list($course->id)) {
    print_error('blocknotavailable', 'block_email_list');
}

// Options for new mail and new folder
$options = new stdClass();
$options->id = $id;
$options->course = $courseid;

if (has_capability('block/email_list:editsettings', $PAGE->context)) {
    $preferencesbutton = email_get_preferences_button($courseid);
}

$stremail  = get_string('name', 'block_email_list');
$params = array('id' => $id, 'course' => $course->id, 'action' => $action);

$PAGE->set_title("$course->shortname: $stremail");
$PAGE->set_heading($course->fullname);
$PAGE->set_cacheable(true);
$PAGE->set_button($preferencesbutton);
$PAGE->set_url('/blocks/email_list/email/folder.php', $params);
$PAGE->requires->css('/blocks/email_list/email/email.css');
$PAGE->requires->css('/blocks/email_list/email/treemenu.css');
$PAGE->requires->css('/blocks/email_list/email/tree.css');
$PAGE->requires->js('/blocks/email_list/email/treemenu.js');
$PAGE->requires->js('/blocks/email_list/email/email.js');

$PAGE->navbar->add(get_string('nameplural', 'block_email_list'), 'index.php?id='.$course->id);
$PAGE->navbar->add(get_string('name', 'block_email_list'), null);

if (isset($folderid)) {

    if (! $folder = $DB->get_record('block_email_list_folder', array('id' => $folderid)) ) {
        print_error('failgetfolder', 'block_email_list');
    }
}

$notifications = array();
$folders       = '';

switch ($action) {
    case md5('admin'):
        list($hassubfolders, $folders) = email_print_administration_folders($options);

        if (! $hassubfolders) {

            // Can create subfolders?
            if ( ! has_capability('block/email_list:createfolder', $PAGE->context)) {
                print_error('forbiddencreatefolder', 'block_email_list',
                            $CFG->wwwroot.'/blocks/email_list/email/index.php?id='.$course->id);
            }

            // Print form to new folder
            $notifications[] = $OUTPUT->notification(get_string('nosubfolders', 'block_email_list'));
            $mform = new folder_form('folder.php', array('id' =>$id, 'course' => $courseid, 'action' => ''));
        }

        break;
    case 'cleantrash':
        $trash = email_get_root_folder($USER->id, EMAIL_TRASH);

        /// If necessary, delete mail and delete attachments
        $options->folderid = $trash->id;

        $success = true;

        $mails = email_get_mails($USER->id, $course->id, null, '', '', $options);

        // Delete reference mails
        if (! $DB->delete_records('block_email_list_foldermail', array('folderid' => $trash->id))) {
            $success = false;
        }

        // Get all trash mails
        if ($mails) {

            foreach ($mails as $mail) {
                $criteria =  array('mailid' => $mail->id);
                // if mailid exist, continue ...
                if ($DB->get_records('block_email_list_foldermail', $criteria)) {
                    continue;
                } else {
                    $deleted = email_delete_attachments($mail->id);
                    // Mail is not reference by never folder (not possibily read)
                    if ($deleted && $DB->delete_records('block_email_list_mail', $criteria)) {
                        $success = true;
                    }
                }

            }
        }

        $url = email_build_url($options);

        // Notify
        if ($success) {
            $notifications[] = $OUTPUT->notification(get_string('cleantrashok', 'block_email_list'));
        } else {
            $notifications[] = $OUTPUT->notification(get_string('cleantrashfail', 'block_email_list'));
        }

        $options->folderid = $id;
        $options->folderoldid = 0;
        $folders = email_showmails($USER->id, '', 0, 10, $options);

        break;

    case md5('edit'):

        // Can create subfolders?
        if (! has_capability('block/email_list:createfolder', $PAGE->context)) {
            print_error('forbiddencreatefolder', 'block_email_list', $CFG->wwwroot
                       .'/blocks/email_list/email/index.php?id='.$course->id);
        }

        $mform = new folder_form('folder.php', array('id' =>$id, 'action'=>$action, 'course' => $courseid));

        $folder = email_get_folder($id);
        $folder->foldercourse = $folder->course;
        unset($folder->course);
        $mform->set_data($folder);

        if ($data = $mform->get_data()) {
            $updatefolder = new stdClass();

            // Clean name
            $updatefolder->name = strip_tags($data->name);

            // Add user and course
            $updatefolder->userid = $USER->id;

            $updatefolder->course = $data->course;

            // Add id
            $updatefolder->id = $data->id;


            /// Update folder

            // Get old folder params
            if (! $oldfolder = $DB->get_record('block_email_list_folder', array('id' => $data->id))) {
                print_error('failgetfolder', 'block_email_list');
            }

            if ($subfolder = email_is_subfolder($oldfolder->id)) {

                // If user changed parent folder
                if ($subfolder->folderparentid != $data->parentfolder) {
                    $criteria = array('id' => $subfolder->id);

                    if (! $DB->set_field('block_email_list_subfolder', 'folderparentid',
                                         $data->parentfolder, $criteria)) {
                        print_error('failchangingparentfolder', 'block_email_list');
                    }
                }
            }

            // Unset parentfolder
            unset($data->parentfolder);

            if ($preference = $DB->get_record('block_email_list_preference', array('userid' => $USER->id))) {

                if ($preference->marriedfolders2courses) {

                    // Change on all subfolders if this course has changed.
                    if ($oldfolder->course != $data->foldercourse) {

                        if ($subfolders = email_get_all_subfolders($data->id)) {

                            foreach ($subfolders as $subfolder0) {
                                $criteria = array('id' => $subfolder0->id);
                                $DB->set_field('block_email_list_folder', 'course', $data->foldercourse, $criteria);
                            }
                        }
                    }
                }
            }

            // Update record
            if (! $DB->update_record('block_email_list_folder', $updatefolder)) {
                return false;
            }

            add_to_log($courseid, 'email_list', 'update subfolder', 'folder.php?id='.$id, "$data->name", 0, $USER->id);

            $notifications[] = $OUTPUT->notification(get_string('modifyfolderok', 'block_email_list'));

            list(, $folders) = email_print_administration_folders($options);
        }

        break;

    case md5('remove'):

        email_removefolder($id, $options);

        list(, $folders) = email_print_administration_folders($options);

    default:

        // Can create subfolders?
        if (! has_capability('block/email_list:createfolder', $PAGE->context)) {
            print_error('forbiddencreatefolder', 'block_email_list',
                        $CFG->wwwroot.'/blocks/email_list/email/index.php?id='.$course->id);
        }

        $mform = new folder_form('folder.php', array('id' =>$id, 'course' => $courseid, 'action' => ''));

        // If the form is cancelled
        if ($mform->is_cancelled()) {

            redirect($CFG->wwwroot.'/blocks/email_list/email/index.php?id='.$courseid,
                     get_string('foldercancelled', 'block_email_list'));

            // Get form sended
        } else if ( $form = $mform->get_data() ) {

            $foldernew = new stdClass();

            // Clean name
            $foldernew->name = strip_tags($form->name);

            // Add user and course
            $foldernew->userid = $USER->id;

            // Add courseid
            if (isset ($form->foldercourse) ) {
                $foldernew->course = $form->foldercourse;
            }

            // Apply this information
            $stralert = get_string('createfolderok', 'block_email_list');

            // Use this field, for known if folder exist o none
            if (! $form->oldname) {
                // Add new folder
                if (! email_newfolder($foldernew, $form->parentfolder)) {
                    print_error('failcreatingfolder', 'block_email_list');
                }

            } else {

                $updatefolder = new stdClass();

                $updatefolder->id = $form->folderid;
                $updatefolder->name = $form->name;
                $updatefolder->parentfolder = $form->parentfolder;
                $updatefolder->course = $form->course;

                // If exist folderid (sending in form), set field
                if (! email_update_folder($updatefolder)) {
                    print_error('failupdatefolder', 'block_email_list');
                }

                // Apply this information
                $stralert = get_string('modifyfolderok', 'block_email_list');
            }

            redirect($CFG->wwwroot.'/blocks/email_list/email/index.php?id='.$courseid, $stralert, '3');

        } else {
            // Set data
            if (isset($folder)) {
                $folder->oldname = $folder->name;
                $parentfolder = email_get_parent_folder($folder);
                $folder->parentfolder = $parentfolder->id;
                $folder->folderid = $folder->id;

                // FIX BUG: When update an folder, on this id has been put $COURSE->id
                $folder->id = $COURSE->id;
            }

        }
}

echo $OUTPUT->header();

// Print principal table. This have 2 columns . . .  and possibility to add right column.
echo '<table id="layout-table"><tr>';


// Print "blocks" of this account
echo '<td style="width: 180px;" id="left-column">';
email_printblocks($USER->id, $courseid);

// Close left column
echo '</td>';

// Print principal column
echo '<td id="middle-column">';

echo implode("\n", $notifications);

// Print block
echo $OUTPUT->heading('');

echo '<div>&#160;</div>';

// Print tabs options
email_print_tabs_options($courseid, 0);

echo $folders;

if (isset($mform)) {
    $mform->display();
}

// Close principal column
echo '</td>';

// Close table
echo "</tr>\n</table>";

echo $OUTPUT->footer();
